package gamecomponents;

import guicomponents.BattleSquare;
import guicomponents.Gui;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class MediumShip implements Ship {

    private int life = 2;
    private static final int size = 2;
    private List position = new ArrayList<Position>();
    private Position correction = new Position(0, 8);
    private boolean TEST;
    private boolean TEST2;
    private static MediumShip INSTANCE;

    private MediumShip() {}

    public static MediumShip getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new MediumShip();
        }
        return INSTANCE;
    }
    
    @Override
    public int getLife() {
        return life;
    }

    @Override
    public boolean getHit(Position coord) {
        coord.correction(correction);
        for (int i = 0; i < position.size(); i++) {
            if (position.get(i) == coord) {
                this.life = this.life - 1;
                return true;
            }
        }
        return false;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public List getPos() {
        return position;
    }

    public void setPos(List position2) {
        this.position = position2;
    }
    
    @Override
    public boolean locAvailable(List<BattleSquare> bs, BattleSquare curBs, boolean VERTICAL) {
        if (VERTICAL) {
            for (int i = 0; i < bs.size(); i++) {
                if (((bs.get(i).getPos().getY() - curBs.getPos().getY()) == 1)
                        && (bs.get(i).getPos().getX() == curBs.getPos().getX()) && bs.get(i).getPos().getY() < 8) {
                    TEST = true;
                }
            }
        } else {
            for (int i = 0; i < bs.size(); i++) {
                if (((bs.get(i).getPos().getX() - curBs.getPos().getX()) == 1)
                        && (bs.get(i).getPos().getY() == curBs.getPos().getY()) && bs.get(i).getPos().getX() < 8) {
                    TEST = true;
                }
            }
        }
        if (TEST) {
            return true;
        } else {
            String message = "Rossz helyre akarod rakni a hajód!";
            JOptionPane.showMessageDialog(null, message, "Játék leírás", 1);
            return false;
        }
    }

    @Override
    public void setLoc(List<BattleSquare> bs, BattleSquare curBs, boolean VERTICAL) {
        if (TEST) {
            if (Gui.getSelectedShip() == 2 && !curBs.getShip()) {
                if (VERTICAL == true) {
                    curBs.setBackground(BattleSquare.Ship_BackgroundColor);
                    curBs.setOnShip(true);
                    position.add(curBs.getPos());
                    for (int i = 0; i < bs.size(); i++) {
                        if (((bs.get(i).getPos().getY() - curBs.getPos().getY()) == 1)
                                && (bs.get(i).getPos().getX() == curBs.getPos().getX())) {
                            bs.get(i).setBackground(BattleSquare.Ship_BackgroundColor);
                            bs.get(i).setOnShip(true);
                            position.add(bs.get(i).getPos());
                        }
                    }
                    this.setPos(position);
                } else {
                    curBs.setBackground(BattleSquare.Ship_BackgroundColor);
                    curBs.setOnShip(true);
                    position.add(curBs.getPos());
                    for (int i = 0; i < bs.size(); i++) {
                        if (((bs.get(i).getPos().getX() - curBs.getPos().getX()) == 1)
                                && (bs.get(i).getPos().getY() == curBs.getPos().getY())) {
                            bs.get(i).setBackground(BattleSquare.Ship_BackgroundColor);
                            bs.get(i).setOnShip(true);
                            position.add(bs.get(i).getPos());
                        }
                    }
                   this.setPos(position);
                }
            }
        }
       
    }
}
